import {Injectable} from '@angular/core';
import {Http, Headers, RequestOptions, RequestOptionsArgs} from '@angular/http';
import {HttpService} from '../../../lib/service/http.service';
import {adSystemApiUrl} from '../../../../config/api';

@Injectable()
export class GoodsAddService {

  constructor(private httpService: HttpService) {
  }


  // 类目
  getCategoryList(level: number, parent: number, name = '', page = 1, pageSize = 999) {
    const url = `${adSystemApiUrl}/v1/member/goods_category/list` +
      `?level=${level}&parent=${parent}&name=${name}&page=${page}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  getCategoryParentById(id) {
    const url = `${adSystemApiUrl}/v1/member/goods_category/findParentById/${id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  getAppletGoodsCategory(applet_uuid, name = '', page = 1, pageSize = 999) {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category/list` +
      `?applet_uuid=${applet_uuid}&name=${name}&page=${page}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  addAppletGoodsCategory(name, applet_uuid, sort = 100, remark = '') {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category/add`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      name: name,
      applet_uuid: applet_uuid,
      sort: sort,
      remark: remark
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  getExpressTemplateList(applet_uuid, name = '', page = 1, pageSize = 999) {
    const url = `${adSystemApiUrl}/v1/member/express_template/list` +
      `?applet_uuid=${applet_uuid}&mame=${name}&page=${page}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  getProvinceList() {
    const url = `${adSystemApiUrl}/v1/member/comm/address/province_list`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  getCityList(province_id) {
    const url = `${adSystemApiUrl}/v1/member/comm/address/city_list?province_id=${province_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  getRegionList(city_id) {
    const url = `${adSystemApiUrl}/v1/member/comm/address/region_list?city_id=${city_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  addGoods(applet_uuid,
           categoryId,
           appletGoodsCategory,
           name, // 商品名称
           goods_bind_attribute,
           stock,
           price,
           retail_Price,
           in_stock,
           goods_number,
           weight,
           // first_picture,
           // second_picture,
           // third_picture,
           // fourth_picture,
           max_buy,
           is_free_postage,
           goods_at_province,
           goods_at_city,
           goods_at_district,
           express_template_id,
           had_invoice,
           had_warranty_fix,
           status,
           description,
           norm_mode,
           is_tc) {
    const url = `${adSystemApiUrl}/v1/member/goods/add`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      applet_uuid: applet_uuid,
      goods_category_id: categoryId,
      appletGoodsCategory: JSON.stringify(appletGoodsCategory),
      goods_bind_attribute: JSON.stringify(goods_bind_attribute),
      name: name,
      stock: JSON.stringify(stock),
      price: price,
      retail_Price: retail_Price,
      in_stock: in_stock,
      weight: weight,
      goods_number: goods_number,
      max_buy: max_buy,
      is_free_postage: is_free_postage,
      goods_at_province: goods_at_province,
      goods_at_city: goods_at_city,
      goods_at_district: goods_at_district,
      express_template_id: express_template_id,
      had_invoice: had_invoice,
      had_warranty_fix: had_warranty_fix,
      status: status,
      description: description,
      norm_mode: norm_mode,
      is_tc: is_tc
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }


  editGoods(id,
            applet_uuid,
            categoryId,
            appletGoodsCategory,
            name, // 商品名称
            goods_bind_attribute,
            stock,
            price,
            retail_Price,
            in_stock,
            goods_number,
            weight,
            // first_picture,
            // second_picture,
            // third_picture,
            // fourth_picture,
            max_buy,
            is_free_postage,
            goods_at_province,
            goods_at_city,
            goods_at_district,
            express_template_id,
            had_invoice,
            had_warranty_fix,
            status,
            description,
            norm_mode,
            is_tc) {
    const url = `${adSystemApiUrl}/v1/member/goods/edit`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      id: id,
      applet_uuid: applet_uuid,
      goods_category_id: categoryId,
      appletGoodsCategory: JSON.stringify(appletGoodsCategory),
      goods_bind_attribute: JSON.stringify(goods_bind_attribute),
      name: name,
      stock: JSON.stringify(stock),
      price: price,
      retail_Price: retail_Price,
      in_stock: in_stock,
      goods_number: goods_number,
      weight: weight,
      max_buy: max_buy,
      is_free_postage: is_free_postage,
      goods_at_province: goods_at_province,
      goods_at_city: goods_at_city,
      goods_at_district: goods_at_district,
      express_template_id: express_template_id,
      had_invoice: had_invoice,
      had_warranty_fix: had_warranty_fix,
      status: status,
      description: description,
      norm_mode: norm_mode,
      is_tc: is_tc
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  getGoodsInfoById(id) {
    const url = `${adSystemApiUrl}/v1/member/goods/${id}/info`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  getAppletGoodsCategoryRelationBindList(goods_id) {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category_relation/bindList?goods_id=${goods_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  getGoodsSkuAttributeBindList(goods_id) {
    const url = `${adSystemApiUrl}/v1/member/goods_sku_attribute/bindList?goods_id=${goods_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  getGoodsSkubindList(goods_id) {
    const url = `${adSystemApiUrl}/v1/member/goods_sku/bindList?goods_id=${goods_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 分类绑定自定义sku
  bindGoodsCategory(goods_category_id,
                    name,
                    data) {
    const url = `${adSystemApiUrl}/v1/member/custom_sku/bindGoodsCategory`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      goods_category_id: goods_category_id,
      name: name,
      data: JSON.stringify(data)
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  getCustomSku(goods_category_id) {
    const url = `${adSystemApiUrl}/v1/member/custom_sku/getCustomSkuByGoodsCategoryId?goods_category_id=${goods_category_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  delCustomSku(id) {
    const url = `${adSystemApiUrl}/v1/member/custom_sku/del`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({id: id,});
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  getCustomSkuByGoodsId(goods_id) {
    const url = `${adSystemApiUrl}/v1/member/custom_sku/getCustomSkuByGoodsId?goods_id=${goods_id}`;
    return this.httpService.httpPostAuth(url, '').map(rs => rs)
  }
}
